#ifndef _PERSONAJE
#define _PERSONAJE

#include "objReader.h"
#include "vec.h"
#include "texture.h"
#include "SampleUtils.h"
#ifdef USE_OPENGL_ES_1_1
#include <GLES/gl.h>
#include <GLES/glext.h>
#else
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#endif

#include <cstdlib>
#include <iostream>

using namespace std;


class drawInfo{
public:
	drawInfo(GLint v, GLint n, GLint t,GLint mvp, float * modelViewMatrix, float * projectionMatrix,float *modelViewProjection, Texture** textures){
		this->v=v;
		this->n=n;
		this->t=t;
		this->mvp=mvp;
		this->modelViewMatrix=modelViewMatrix;
		this->projectionMatrix=projectionMatrix;
		this->modelViewProjection=modelViewProjection;
		this->textures=textures;
	}
GLint v; GLint n; GLint t;GLint mvp; float * modelViewMatrix; float * projectionMatrix;float *modelViewProjection;
Texture** textures;

};


animacion *personaje3d=new animacion("./sdcard/juego/anim/");

float matrizRot[16];

void copiarMatriz(float * m){
	for(int i=0;i<16;i++)matrizRot[i]=m[i];
}

class personaje{
public:
	//Attributes
	vec2 *pos;
	vec2* destino;
	float angulo;
	int slice;
	//Values for game logic
	bool isDead;
	int timerValue;
	int timerValueLimit;
	bool isInitialized;

	personaje(){
		isDead = true;
		isInitialized = false;
		timerValueLimit = 120;
		timerValue=0;
		pos=new vec2(0,0);
		destino=new vec2(-112, -160);
		slice=rand();
		
		destino->x=(rand()%224 ) - 112;
		destino->y=(rand()%320 ) - 160;

		//	LOG("%f %f", destino->x,destino->y);
	}


 void draw(){

	 if(isDead)
		 return;

	if(pos->dist(*destino)<1.0f){
		destino->x=(rand()%224 ) - 112;
		destino->y=(rand()%320 ) - 160;

	}
	
	float min=0.4, max=1;

	float dx=destino->x-pos->x;
	float dy=destino->y-pos->y;
	
	


	if(abs(dx)>max)dx=max*dx/abs(dx);
	if(abs(dy)>max)dy=max*dy/abs(dy);
	pos->x+=dx;
	pos->y+=dy;
	

	vec2 dif(dx,dy);
	float ang=dif.angulo();
	angulo=ang;

glBindTexture(GL_TEXTURE_2D, 1);
float scale=10.0f;  
slice++;
personaje3d->setInd((int)slice);

#ifndef USE_OPENGL_ES_1_1
	
	copiarMatriz(info.modelViewMatrix);
		
	QCAR::Matrix44F modelViewProjection;
    SampleUtils::translatePoseMatrix(pos->x, pos->y, 0.0f,matrizRot);
	
	SampleUtils::scalePoseMatrix(scale, scale, scale,matrizRot);
    SampleUtils::rotatePoseMatrix(angulo, 0.0f, 0.0f, 1.0f,matrizRot);
	SampleUtils::multiplyMatrix(info.projectionMatrix, matrizRot ,&modelViewProjection.data[0]);

	glUniformMatrix4fv(info.mvp, 1, GL_FALSE,(GLfloat*)&modelViewProjection.data[0] );
	
	
	
	personaje3d->draw(info.v,info.n,info.t);

	glUniformMatrix4fv(info.mvp, 1, GL_FALSE,(GLfloat*)info.modelViewProjection);
#else
	
		glPushMatrix();
        glTranslatef(pos->x, pos->y, 0.0f);
        glScalef(scale, scale, scale);
		glRotatef (angulo, 0.0f, 0.0f,1.0f);

		personaje3d->draw();
		glPopMatrix();

#endif

 }

 void Kill()
 {
	 isDead = true;
	 timerValue = 0;
 }

 void UpdateTimerLimit(int newValue)
 {
	 timerValueLimit = newValue;
 }

 void WaitForRecover()
 {
	 timerValue++;
	 if(isDead && timerValue >= timerValueLimit && isInitialized){
		isDead = false;
	}
 }

 bool IsAlive()
 {
	 return !isDead;
 }

 void Start()
 {
	 isInitialized = true;
	 timerValue=0;
 }

 bool IsInitialized()
 {
	 return isInitialized;
 }





};



#endif
